package com.stylefeng.guns.modular.pic.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.item.model.Item;
import com.stylefeng.guns.modular.item.util.Const;
import com.stylefeng.guns.modular.item.util.ItemWarpper;
import com.stylefeng.guns.modular.pic.util.PicWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.pic.model.Pic;
import com.stylefeng.guns.modular.pic.service.IPicService;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 图片管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-28 09:47:37
 */
@Controller
@RequestMapping("/pic")
public class PicController extends BaseController {

    private String PREFIX = "/pic/pic/";

    @Autowired
    private IPicService picService;

    /**
     * 跳转到图片管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "pic.html";
    }

    /**
     * 跳转到添加图片管理
     */
    @RequestMapping("/pic_add")
    public String picAdd(Model model) {
        List list = Const.getPicModelList();
        model.addAttribute("list",list);
        return PREFIX + "pic_add.html";
    }

    /**
     * 跳转到修改图片管理
     */
    @RequestMapping("/pic_update/{picId}")
    public String picUpdate(@PathVariable Integer picId, Model model) {
        Pic pic = picService.selectById(picId);
        List list = Const.getPicModelList();
        model.addAttribute("item",pic);
        model.addAttribute("list",list);
        LogObjectHolder.me().set(pic);
        return PREFIX + "pic_edit.html";
    }

    /**
     * 获取图片管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Pic> list =  picService.selectList(null);
        List<Map<String, Object>> list1  = BeanKit.listToMapList(list);
        return new PicWarpper(list1).warp();
    }

    /**
     * 新增图片管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Pic pic) {
        Date date = new Date();
        pic.setDate(date);
        picService.insert(pic);
        return SUCCESS_TIP;
    }
    /***
     * 点击查看详情
     * **/
    @RequestMapping(value = "/detailYang/{itemId}")
    public String detailYang(@PathVariable("itemId") Integer itemId,Model model){
        Pic pic = picService.selectById(itemId);
        model.addAttribute("item",pic);
        return PREFIX + "pic_detail.html";
    }

    /**
     * 删除图片管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer picId) {
        picService.deleteById(picId);
        return SUCCESS_TIP;
    }

    /**
     * 修改图片管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Pic pic) {
        picService.updateById(pic);
        return SUCCESS_TIP;
    }

    /**
     * 图片管理详情
     */
    @RequestMapping(value = "/detail/{picId}")
    @ResponseBody
    public Object detail(@PathVariable("picId") Integer picId) {
        return picService.selectById(picId);
    }
}
